/**  

* @Title: AdministratorDaoHibernate.java 

* @Package com.gxbadge.dao.impl 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.dao.impl;

import java.util.List;

import com.gxbadge.dao.ArticleDao;
import com.gxbadge.dao.base.BaseHibernateDaoSupport;
import com.gxbadge.model.Article;
import com.gxbadge.model.Navigation;

public class ArticleDaoHibernate extends BaseHibernateDaoSupport implements ArticleDao {

	/* 
	 * {见接口方法说明}
	 */
	public void delete(Article article) {
		
		this.getHibernateTemplate().delete(article);
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public List<Article> findArticleAll() {
		
		return null;
	}

	public List<Article> findArtiListByNav(Navigation nav){
		
		return null;
	}
	
	/* 
	 * {见接口方法说明}
	 */
	public Article findArticleByArtiId(Integer artiId) {
		
		Article article = this.getHibernateTemplate().get(Article.class, artiId);
		if(article!=null){
			return article;
		}
		return null;
	}

	/* 
	 * {见接口方法说明}
	 */
	public void save(Article article) {
		
		this.getHibernateTemplate().save(article);
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public void update(Article article) {
		
		this.getHibernateTemplate().update(article);
		
	}
	
	public List<Article> findArtiListByNavList(List<Navigation> navList, int offset, int pageSize){
		
		
		Object[] values = new Object[navList.size()];
		for(int i=0;i<navList.size();i++){
			values[i] = navList.get(i);
		}
		String hql_param = "";
		for(int i=0;i<navList.size();i++){
			if(i==navList.size()-1){
				hql_param += "a.nav=?";
			}else{
				hql_param += "a.nav=? || ";
			}
			
		}
		String hql = "from Article as a where a.deleteFlag=false and ("+ hql_param +") order by a.uploadDate desc";
		
		List<Article> artiList = this.findByPage(hql, values, offset, pageSize);
		values = null;
		hql = null;
		hql_param = null;
		if(artiList!=null&&artiList.size()>=1){
			
			return artiList;
		}
		return null;
	}
	
	public long find_artiTotalCount(List<Navigation> navList){
		
		Object[] values = new Object[navList.size()];
		for(int i=0;i<navList.size();i++){
			values[i] = navList.get(i);
		}
		String hql_param = "";
		for(int i=0;i<navList.size();i++){
			if(i==navList.size()-1){
				hql_param += "a.nav=?";
			}else{
				hql_param += "a.nav=? || ";
			}
			
		}
		String hql = "select count(*) from Article as a where a.deleteFlag=false and ("+ hql_param +")";
		long artiTotalCount = (Long)this.getHibernateTemplate().find(hql, values).listIterator().next();
		values = null;
		hql = null;
		hql_param = null;
		return artiTotalCount;
	}

	/* ------------------文章模块处理-------前台-----------    */                    
	public List<Article> findFrontArtiListByNavList(List<Navigation> navList, int offset, int pageSize){
		
		Object[] values = new Object[navList.size()];
		for(int i=0;i<navList.size();i++){
			values[i] = navList.get(i);
		}
		String hql_param = "";
		for(int i=0;i<navList.size();i++){
			if(i==navList.size()-1){
				hql_param += "a.nav=?";
			}else{
				hql_param += "a.nav=? || ";
			}
			
		}
		String hql = "select a.artiId, a.title,a.uploadDate from Article as a where a.deleteFlag=false and ("+ hql_param +") order by a.uploadDate desc";
		
		List<Article> artiList = this.findByPage(hql, values, offset, pageSize);
		
		values = null;
		hql = null;
		hql_param = null;
		
		if(artiList!=null&&artiList.size()>=1){

			return artiList;
		}
		return null;
		
		
	}
	
	public long ffind_artiTotalCount(List<Navigation> navList){
		
		Object[] values = new Object[navList.size()];
		for(int i=0;i<navList.size();i++){
			values[i] = navList.get(i);
		}
		String hql_param = "";
		for(int i=0;i<navList.size();i++){
			if(i==navList.size()-1){
				hql_param += "a.nav=?";
			}else{
				hql_param += "a.nav=? || ";
			}
			
		}
		String hql = "select count(*) from Article as a where a.deleteFlag=false and ("+ hql_param +")";
		long artiTotalCount = (Long)this.getHibernateTemplate().find(hql, values).listIterator().next();
		values = null;
		hql = null;
		hql_param = null;
		return artiTotalCount;
		
	}

}
